<?php
namespace Home\Controller;

use Workerman\Worker;
use Workerman\Autoloader;
use GatewayWorker\Gateway;
use GatewayWorker\BusinessWorker;
use GatewayWorker\Register;
use Home\Controller\EventsController as Events;

class SocketController{

    public function __construct(){
        
    }

    public function start(){
        $this->startRegister();
        $this->startBusinessWorker();
        $this->startGateWay();
        Worker::runAll();
    }

    private function startRegister(){
        // new Register('text://192.168.10.149:1237');//本地
        new Register('text://0.0.0.0:1238');//正式
    }

    private function startBusinessWorker(){
        $worker                  = new BusinessWorker();
        $worker->name            = 'BusinessWorker';// worker名称
        $worker->count           = 1;// bussinessWorker进程数量
        // $worker->registerAddress = '192.168.10.149:1237';// 本地服务注册地址
        $worker->registerAddress = '127.0.0.1:1238';// 正式服务注册地址
        $worker->eventHandler    = Events::class;//设置处理业务的类,此处制定Events的命名空间
    }

    private function startGateWay(){
        // $gateway = new Gateway("Websocket://0.0.0.0:7272");// 本地 gateway 进程
        $gateway = new Gateway("websocket://0.0.0.0:8282");// 正式 gateway 进程
        $gateway->name            = 'Gateway';// 设置名称，方便status时查看
        $gateway->count           = 4;// 设置进程数，gateway进程数建议与cpu核数相同
        // $gateway->lanIp           = '127.0.0.1';// 本地分布式部署时请设置成内网ip（非192.168.10.149）
        $gateway->lanIp           = '127.0.0.1';// 正式分布式部署时请设置成内网ip（非192.168.10.149）
        $gateway->startPort       = 2900;// 内部通讯起始端口，假如$gateway->count=4，起始端口为4000
        $gateway->pingInterval    = 10;// 心跳间隔
        $gateway->pingData        = '{"type":"ping"}';// 心跳数据
        // $gateway->registerAddress = '192.168.10.149:1237';// 本地服务注册地址
        $gateway->registerAddress = '127.0.0.1:1238';// 正式服务注册地址
    }

}